package com.woniuxy.finance.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.common.util.CheckTokenUtil;
import com.woniuxy.finance.model.dto.ConditionAccDto;
import com.woniuxy.finance.model.dto.FindBlotterDto;
import com.woniuxy.finance.model.dto.RechargeDto;
import com.woniuxy.finance.model.dto.UpdateRechargeDto;
import com.woniuxy.finance.model.entity.Blotter;
import com.woniuxy.finance.model.entity.Recharge;
import com.woniuxy.finance.model.vo.CountClientVo;
import com.woniuxy.finance.model.vo.FindUpdateVo;
import com.woniuxy.finance.service.RechargeService;
import com.woniuxy.common.model.entity.AccountManage;
import com.woniuxy.response.ResponseResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 客户充值表; 前端控制器
 * </p>
 *
 * @author chenlin
 * @since 2023-09-06
 */
@RestController
@RequestMapping("/finance")
public class RechargeController {
    @Resource
    private RechargeService rechargeService;

    /**
     * 客户充值管理接口
     */
    /**
     * 添加充值/退款
     */
    @PreAuthorize("hasAuthority('finance:addMoney')")
    @PostMapping("/addRecharge")
    public ResponseResult<Boolean> addRecharge(@RequestBody Recharge recharge, HttpServletRequest request){
        String u = request.getHeader("access_token");
        Map<String, Object> user = CheckTokenUtil.getUser(u);
//        Integer aid = (Integer) user.get("uid");
        String account = (String) user.get("account");
        boolean result = rechargeService.addRecharge(recharge,account);
        if(result) {
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",true);
        }else{
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"fail",false);
        }
    }
    /**
     * 查询充值单
     */
    @GetMapping("/findRecharge/{page}/{size}")
    public ResponseResult<Page<Recharge>> findRecharge(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        return new ResponseResult<>(ResponseResult.SUCCESS,"SUCCESS",rechargeService.findRecharge(page,size));
    }
    /**
     * 多条件查询充值
     */
    @PostMapping("/conditions")
    public ResponseResult<Page<Recharge>> conditions(@RequestBody RechargeDto rechargeDTO){
        System.out.println(rechargeDTO.getRechargeNum());
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",rechargeService.conditions(rechargeDTO));
    }
    /**
     * 根据id修改充值单
     */
    @PreAuthorize("hasAuthority('finance:updateList')")
    @PostMapping("/updateRecharge")
    public ResponseResult<Page<Recharge>> updateRecharge(@RequestBody UpdateRechargeDto dto, HttpServletRequest request){
        String u = request.getHeader("access_token");
        Map<String, Object> user = CheckTokenUtil.getUser(u);
        String account = (String) user.get("account");
        Recharge recharge = new Recharge();
        recharge.setId(dto.getId());
        recharge.setMoney(dto.getMoney());
        recharge.setAdminName(account);
        Boolean result = rechargeService.updateRecharge(recharge);
        if(result) {
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",null);
        }else{
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"fail",rechargeService.findRecharge(dto.getPage(),dto.getSize()));
        }
    }
    /**
     * 查询充值修改记录
     */
    @GetMapping("/findUpdate/{id}")
    public ResponseResult<List<FindUpdateVo>> findUpdate(@PathVariable("id") Integer id){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",rechargeService.findUpdate(id));
    }
    /**
     * 根据id删除充值单
     */
    @PreAuthorize("hasAuthority('finance:updateList')")
    @DeleteMapping("/delRecharge/{id}/{page}/{size}")
    public ResponseResult<Page<Recharge>> delRecharge(@PathVariable("id") Integer id,@PathVariable("page") Integer page, @PathVariable("size") Integer size){
       Boolean result = rechargeService.delRecharge(id);
        if(result) {
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",rechargeService.page(new Page<>(page,size)));
        }else{
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"fail",null);
        }
    }

    /**
     * 客户账户管理接口
     */
    /**
     * 获取所有账户管理信息
     */
    @GetMapping("/findAccountManage/{page}/{size}")
    public ResponseResult<Page<AccountManage>> findAccountManage(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",rechargeService.findAccountManage(page,size));
    }
    /**
     * 添加一条账户管理信息
     */
    @PostMapping("/addAccountManage")
    public ResponseResult<Boolean> addAccountManage(@RequestBody AccountManage accountManage){
        boolean result = rechargeService.addAccountManage(accountManage);
        if(result) {
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",true);
        }else{
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"fail",false);
        }
    }
    /**
     * 多条件查询账户
     */
    @PostMapping("/conditionsAccount")
    public ResponseResult<Page<AccountManage>> conditionsAccount(@RequestBody ConditionAccDto accDto){
        System.out.println(accDto);
        String userName = accDto.getUserName();
        String aLock = accDto.getAbc();
        System.out.println(aLock);
        Integer page = accDto.getPage();
        Integer size = accDto.getSize();
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",rechargeService.conditionsAccount(userName,aLock,page,size));
    }
    /**
     * 锁定
     */
    @PreAuthorize("hasAuthority('finance:lockaccout')")
    @PutMapping("/lock/{id}/{page}/{size}")
    public ResponseResult<Page<AccountManage>> lock(@PathVariable("id") Integer id,@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        boolean result = rechargeService.lock(id);
        if(result) {
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",rechargeService.findAccountManage(page,size));
        }else{
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"fail",null);
        }
    }
    /**
     * 解除锁定
     */
    @PreAuthorize("hasAuthority('finance:lockaccout')")
    @PutMapping("/unlock/{id}/{page}/{size}")
    public ResponseResult<Page<AccountManage>> unlock(@PathVariable("id") Integer id,@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        boolean result = rechargeService.unlock(id);
        if(result) {
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",rechargeService.findAccountManage(page,size));
        }else{
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"fail",null);
        }
    }
    /**
     * 扣除账户应缴费用
     */
    @PreAuthorize("hasAuthority('finance:updatemoney')")
    @PutMapping("/updateMoney/{id}/{page}/{size}")
    public ResponseResult<Page<AccountManage>> updatemoney(@PathVariable("id") Integer id,@PathVariable("page") Integer page, @PathVariable("size") Integer size,HttpServletRequest request){
        String u = request.getHeader("access_token");
        Map<String, Object> user = CheckTokenUtil.getUser(u);
        String aname = (String) user.get("account");
        System.out.println(aname);
        boolean result = rechargeService.updatemoney(id,aname);
        if(result) {
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",rechargeService.findAccountManage(page,size));
        }else{
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"fail",null);
        }
    }
    /**
     * 统计客户信息和充值总额
     */
    @GetMapping("/countClientData/{page}/{size}")
    public ResponseResult<IPage<CountClientVo>> countClientData(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",rechargeService.countClientData(page,size));
    }
    /**
     * 查询友商流水信息 分页
     */
    @PostMapping("/findBlotter")
    public ResponseResult<IPage<Blotter>> findBlotter(@RequestBody FindBlotterDto blotterDto){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",rechargeService.findBlotter(blotterDto));
    }
}

